package com.jstyle.jclife.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.pdf.PdfDocument;


import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.model.UserInfo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

public class PDFCreate {
    private static final String TAG = "PDFCreate";
    final static int maxXSize = 5120;
      static float  length = 20;
     static float totalWidth = 1070;
     static int startX = 35;
     static float endX = startX + 1000;
     static float startY ;
    private static float endY;
    private  static float height ;
    private  static float width ;
    private static int strokeWidthTime;
    private static int strokeWidthLine;
    private static float totalHeight;

    public static void createPdf(String path, Context context, List<Double> data, String date) {
        int size = data.size();
        float col = size % maxXSize == 0 ? size / maxXSize : size / maxXSize + 1;//一次画5120个点，分多少次画
        length=ScreenUtils.dip2px(context,10);
        height=length;
        width=length;
        startX=ScreenUtils.dip2px(context,20);
        endX=startX+50*length;
        totalWidth=endX+startX;
        totalHeight = col * height * 5 + ScreenUtils.dip2px(context,130);//一次需要5个高度
        PdfDocument pdfDocument = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder((int) totalWidth, (int) totalHeight, 1)
                // .setContentRect(new Rect(0,0,(int)totalWidth,(int)totalHeight))
                .create();
        PdfDocument.Page page = pdfDocument.startPage(pageInfo);

        Canvas canvas = page.getCanvas();
        Paint paint = new Paint();
        strokeWidthTime = ScreenUtils.dip2px(context, 1.5f);
        strokeWidthLine = ScreenUtils.dip2px(context, 0.5f);
        drawReportInfo(context, canvas, paint, date);
        Path pathCanvas = new Path();
        endY = startY + col * height * 5;
        paint.setTextSize(ScreenUtils.dip2px(context, 15));
        drawAxes(pathCanvas, canvas, paint, col);
        drawTimeLine(pathCanvas, canvas, paint);
        drawDataLine(context,pathCanvas, canvas, paint, col, data);
        pdfDocument.finishPage(page);
        File file = new File(path);
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            pdfDocument.writeTo(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**用户信息
     * @param context
     * @param canvas
     * @param paint
     * @param date
     */
    private static void drawReportInfo(Context context, Canvas canvas, Paint paint, String date) {
        paint.setTextSize(ScreenUtils.dip2px(context,15));
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
        UserInfo userInfo = UserInfoDaoManager.getUserByUid(NetWorkUtil.getUserId());
        String name = userInfo.getName();
        int gender = userInfo.getGender();
        String genderString = gender == 0 ? context.getString(R.string.male) : context.getString(R.string.female);
        String birthday = userInfo.getBirthday();

        Calendar calendar = Calendar.getInstance();
        String[]birthdayArray=birthday.split("-");
        int year= Integer.parseInt(birthdayArray[0]);
        int month= Integer.parseInt(birthdayArray[1]);
        int day= Integer.parseInt(birthdayArray[2]);
        int nowYear=calendar.get(Calendar.YEAR);
        int nowMonth=calendar.get(Calendar.MONTH)+1;
        int nowDay=calendar.get(Calendar.DAY_OF_MONTH);
        int age = 0;
        if(nowMonth-month>=0&&nowDay-day>=0){
            age = nowYear-year;
        }else{
            age = nowYear-year-1;
        }
        int userWeight = userInfo.getWeight();
        int userHeight = userInfo.getHeight();
        String title=context.getString(R.string.ECG_Report_Lead);
        Rect rect = new Rect();
        paint.getTextBounds(title, 0, title.length(), rect);
        int heightTitle = rect.height();
        float startTextY=heightTitle+ScreenUtils.dip2px(context,8);
        float marginTop=ScreenUtils.dip2px(context,4);
        float marginRight=ScreenUtils.dip2px(context,8);
        //标题
        canvas.drawText(title, totalWidth / 2 - 150, startTextY, paint);

        //提醒
        paint.setTextSize(ScreenUtils.dip2px(context,12));
        String tips=context.getString(R.string.ecg_report_tips);
        float tipsTextY=startTextY+heightTitle+marginTop;
        paint.getTextBounds(tips, 0, tips.length(), rect);
        int heightTips = rect.height();
        int widthTips = rect.width();
        canvas.drawText(tips, totalWidth - widthTips-marginRight, tipsTextY, paint);

        //个人信息
        float infoTextY=tipsTextY+heightTips+marginTop;
        String info=String.format(context.getString(R.string.report_info),name,genderString,String.valueOf(age),
                String.valueOf(userHeight),String.valueOf(userWeight));
//        String info = "Name: " + name + " " + " Gender: " + genderString +
//                " Age: " + age + " Height: " + userHeight + "cm " + "Weight: " + userWeight + "kg";
        paint.getTextBounds(info, 0, info.length(), rect);
        int heightInfo = rect.height();
        int widthInfo = rect.width();
        canvas.drawText(info, totalWidth / 2 - widthInfo/2, infoTextY, paint);
        //时间
        float dateTextY=infoTextY+heightInfo+marginTop;
        String dateString=context.getString(R.string.date) + date;
        paint.getTextBounds(dateString, 0, dateString.length(), rect);
        int widthDate=rect.width();
        int heightDate=rect.height();
        canvas.drawText(dateString, totalWidth - widthDate-marginRight, dateTextY, paint);
        startY=dateTextY+heightDate+marginTop;

    }

    /**
     * 画网格
     *
     * @param pathCanvas
     * @param canvas
     * @param paint
     * @param col
     */
    private static void drawAxes(Path pathCanvas, Canvas canvas, Paint paint, float col) {
        //画网格竖线
        int colorLine = Color.rgb(243, 119, 99);
        paint.setColor(colorLine);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidthLine);
        paint.setAlpha(200);
        for (int i = 0; i <51; i++) {
            pathCanvas.moveTo(i * width + startX, startY);
            pathCanvas.lineTo(i * width + startX, endY);
        }
        //画网格横线
        for (int i = 0; i <= col * 5; i++) {
            pathCanvas.moveTo(startX - strokeWidthTime / 2, i * height + startY);
            pathCanvas.lineTo(endX + strokeWidthTime / 2, i * height + startY);
        }
        canvas.drawPath(pathCanvas, paint);
        pathCanvas.reset();
    }

    /**
     * 加粗时间线
     *
     * @param pathCanvas
     * @param canvas
     * @param paint
     */
    private static void drawTimeLine(Path pathCanvas, Canvas canvas, Paint paint) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(strokeWidthTime);
        paint.setStyle(Paint.Style.FILL);
        Rect rect=new Rect();
        for (int i = 0; i < 11; i++) {
            pathCanvas.moveTo(i * 5 * width + startX, startY);
            pathCanvas.lineTo(i * 5 * width + startX, endY + 20);
            String time=i+"s";
            paint.getTextBounds(time, 0, time.length(), rect);
            int widthTime=rect.width();
            canvas.drawText(time, i * 5 * width + startX - widthTime/2, endY + 50, paint);
        }
        int colorS = Color.rgb(255, 119, 99);
        paint.setColor(colorS);
        paint.setAlpha(255);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawPath(pathCanvas, paint);
        pathCanvas.reset();
    }

    /**
     * 画数据
     *
     * @param pathCanvas
     * @param canvas
     * @param paint
     * @param col
     * @param data
     */
    private static void drawDataLine(Context context,Path pathCanvas, Canvas canvas, Paint paint, float col, List<Double> data) {
        int size = data.size();
        paint.setStrokeWidth(strokeWidthLine);
        paint.setColor(Color.BLACK);
        out:
        for (int i = 0; i < col; i++) {
            int startPoint = i * maxXSize;
           in: for (int j = 0; j <maxXSize; j++) {
                int x = j;
                if (startPoint + j >= size) break out;
                Double y = data.get(startPoint + j);
                if (j == 0) pathCanvas.moveTo(startX, i * height * 5 + getCanvasY(y,context));
                pathCanvas.lineTo(startX + x * (endX - startX) / maxXSize, i * height * 5 + getCanvasY(y,context));
            }
        }

        canvas.drawPath(pathCanvas, paint);
    }

    private static float getCanvasY(double value,Context context) {
        double height = 5 * 10 / 16000f;

        return  ScreenUtils.dip2px (context,(float) (height * (8000 - value))) + startY;
    }
}
